<?php
class MessagesController extends AppController {


/**
 * Uses array
 *
 * @var array
 */
    public $uses = array('Message');

/**
 * Helpers
 *
 * @var array
 **/
  public $components = array('RequestHandler', 'SendMail',"Emstraining");
  //public $helpers = array('Html', 'Form', 'Paginator', 'Js' => array('Jquery'));

/**
 * Pagination
 *
 * @var array
 **/
  public $paginate = array(
        'order' => array('id' => 'asc'),
        'limit' => 15
  );
    
  public function beforeFilter() {
      parent::beforeFilter();
  }

/**
 * @method: index
 * @purpose: list of all tickets
 * @param none
 * @return void
 **/
  public function admin_index() {
      $this->set('messages', $this->paginate('Message'));
  }
    
/**
 * @method: add 
 * @purpose: admin to add template
 * @param none
 * @return void
 **/
    public function admin_add() {
        if ($this->request->is('post')) {
            $this->request->data['Message']['created_by']=  $this->Auth->user("id");
            $this->request->data['Message']['is_deleted']=1;
            if ($this->Message->save($this->request->data)) {
                $this->Session->setFlash('Template has been saved.');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Could not save template, try later.');
            }
        }
    }
    
/**
 * @method: detail
 * @purpose: detail of enquiry ticket
 * @param none
 * @return void
 **/
  public function admin_view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid template', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('message', $this->Message->read(null, $id));
  }
  
/**
 * @method: reply
 * @purpose: reply to enquiry ticket
 * @param none
 * @return void
 **/
  public function admin_edit($id = null){
        if (!$id && empty($this->request->data)) {
            $this->Session->setFlash(__('Invalid Template', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->request->is('post') || $this->request->is('put')) { 
            if ($this->Message->save($this->data)) {
                $this->Session->setFlash(__('The template has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The template could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->request->data)) {
            $this->request->data = $this->Message->read(null, $id);
        }
  }
  
  
/**
 * @method: admin_delete
 * @purpose: admin to delete template
 * @param none
 * @return void
 **/
    function admin_delete($id = null) {
        if (!$id) {
                $this->Session->setFlash(__('Invalid id for template', true));
                $this->redirect(array('action'=>'index'));
        }
        if ($this->Message->delete($id)) {
                $this->Session->setFlash(__('Template deleted', true));
                $this->redirect(array('action'=>'index'));
        }
        $this->Session->setFlash(__('Template was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }
       
/**
 * @method: send
 * @purpose: admin to send msg to customer using template
 * @param none
 * @return void
 **/
    function admin_send($customer=null) {
        $this->loadModel('Customer');
        $this->loadModel("CustomerType");
        $this->loadModel('ShopperGroup');
        $this->loadModel('CustomerShopperGroup');
        if($this->request->is('post')){  
            //pr($this->data);die;
            if($this->data['Message']['send_to']=="customer"){
                $customerIDs = $this->request->data['Message']['customer'];
            }else if($this->data['Message']['send_to']=="type"){
                $types = $this->request->data['Message']['types'];
                $customerIDs = array_keys($this->Customer->find('list',array('conditions'=>array('customer_type_id'=>$types))));
            }else if($this->data['Message']['send_to']=="group"){
                $groups = $this->request->data['Message']['groups'];
                $customer_list = $this->CustomerShopperGroup->find('all',array('conditions'=>array('shopper_group_id'=>$groups)));
                $customerIDs = array();
                foreach($customer_list as $cus){
                    if(!in_array($cus['Customer']['id'], $customerIDs)){
                        $customerIDs[]=$cus['Customer']['id'];
                    }
                }
                
            }
            //pr($customerIDs);die;
            $template = $this->request->data['Message']['template_id'];
            $subject = $this->request->data['Message']['subject'];
            $content = $this->request->data['Message']['message'];
            if(!empty($customerIDs)){

                $customers = array_values($this->Customer->find('list', array('conditions' => array('Customer.id' => $customerIDs), 'fields' => array('Customer.email'))));
                $emails = implode(',', $customers);
                App::import("View", "View");
                $view = new View();
                $absolute_path = Router::url('/', true);
                $content = str_replace("\n", "<br/>", $content);
                $content =  $view->element("email_template", array('content'=>$content,'js_absolute_path'=>$absolute_path));
                $this->SendMail->send_message($customers, $subject, $content);
                $this->Session->setFlash(__('Message has been sent.', true));
                $this->redirect(array('action' => 'index'));
            }else{
                $this->Message->invalidate('customer',"You have no choose any customer or no customer in groups or type that you choose");
            }
        }

        $templates = $this->Message->find('all', array('conditions' => array('Message.is_deleted' => '1')));
        $customers = $this->Customer->find('all');
        $customer_select = array();
        if ($customer) {
            $customer_select = explode(",", $customer);
        }
        
        //pr($customer_select);die;
        
        
        $this->set('customer_types',$this->CustomerType->get_list());
        $this->set('customer_groups',$this->ShopperGroup->get_list());
        
        $this->set('customer_select', $customer_select );
        $this->set('templates', $templates);
        $this->set('customers', $customers);
    }   
  
/**
 * @method: send
 * @purpose: admin to send msg to customer using template
 * @param none
 * @return void
 **/
    function admin_load_template($id = null,$language=null) {
        if ($this->request->is('ajax')) {
           $template = $this->Message->find('first', array('conditions' => array('Message.id' => $id)));
           $message=$template['Message']['message_en'];
           if ($language=="Arabic") {
               $message=$template['Message']['message_ar'];
           }
           $this->set('message', $message);
        }
    }
}